package com.xykj.deploy.login.controller;

import com.xykj.deploy.login.entity.UserInfo;
import com.xykj.deploy.login.service.UserService;
import com.xykj.deploy.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: ethan
 * @Date: 2024/11/28
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
public class LoginController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result<?> login(@RequestBody @Validated UserInfo user, HttpServletResponse response) {
        String token = userService.login(user, response);
        if (token != null) {
            return Result.ok("登录成功", token);
        }
        return Result.error("登录失败");
    }

    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("Authorization") String token) {
        userService.logout(token.replace("Bearer ", ""));
        return Result.ok("退出成功");
    }
}
